package cn.stylefeng.guns.modular.wms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.wms.entity.WorkExp;
import cn.stylefeng.guns.modular.wms.mapper.WorkExpMapper;
import cn.stylefeng.guns.modular.wms.model.params.WorkExpParam;
import cn.stylefeng.guns.modular.wms.model.result.WorkExpResult;
import  cn.stylefeng.guns.modular.wms.service.WorkExpService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 工作经历表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2021-03-23
 */
@Service
public class WorkExpServiceImpl extends ServiceImpl<WorkExpMapper, WorkExp> implements WorkExpService {

    @Override
    public void add(WorkExpParam param){
        WorkExp entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(WorkExpParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(WorkExpParam param){
        //判断ID是否存在，如果不存在，则新建
        if (param.getId()!=null){
            WorkExp oldEntity = getOldEntity(param);
            WorkExp newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }else{
            this.add(param);
        }
    }

    @Override
    public WorkExpResult findBySpec(WorkExpParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<WorkExpResult> findListBySpec(WorkExpParam param){
        return baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(WorkExpParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(WorkExpParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private WorkExp getOldEntity(WorkExpParam param) {
        return this.getById(getKey(param));
    }

    private WorkExp getEntity(WorkExpParam param) {
        WorkExp entity = new WorkExp();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
